<?php
/**
 * 菜单Model类
 * @file 	MenuModel.class.php
 * @author 	itkele@gmail.com
 * @date 	2013-06-13
 * modify       
 */
class MenuModel extends Model{
	
	private $table;
	
	public function __construct(){
		parent::__construct();
		
		$this->table = C('db_table_prefix').'menu';
	}
	
	/**
	 * 添加菜单
	 * @param array $data
	 * @return boolean
	 */
	public function addMenu($data){
		foreach($data as $key => $val){
			$val = trim($val);
			$data[$key] = addslashes($val);
		}
		
		$menutype = $data['menutype'];
		$title = $data['title'];
		$alias = $data['alias'];
		$link = $data['link'];
		$type = $data['type'];
		$parent_id = $data['parent_id'];
		$component_id = $data['component_id'];
		$ordering = $data['ordering'];
		$params = $data['params'];
		$ctime = $utime = time();
		$title = $data['title'];
		$status = $data['status'];
		
		$sql = "INSERT INTO `{$this->table}`
			(`id`,`menutype`,`title`,`alias`,`link`,`type`,`parent_id`,`component_id`,`ordering`,`params`,`ctime`,`utime`,`status`) VALUES 
			(NULL,'{$menutype}','{$title}','{$alias}','{$link}','{$type}','{$parent_id}','{$component_id}','{$ordering}','{$params}','{$ctime}','{$utime}','{$status}')";
		$result = $this->execute($sql);
		
		return $result;
	}
	
	/**
	 * 获取获取全部或单个菜单
	 * @param 	int $id 
	 * @return 	array $menuList
	 */
	public function menuList($id=null){
		$menuList = array();
		$sql = "SELECT * FROM `{$this->table}` WHERE `status`=1";
		if($id){
			$sql .= " AND `id`='{$id}'";
		}
		$sql .= " ORDER BY `ordering` ASC,`id` ASC";
		$menuList = $this->query($sql);
		
		return $menuList;
	}
	
	//---------------------debug----------------------------
	
	/**
	 * 获取单条菜单信息
	 */
	public function getCategoryById($id){
		if(empty($id)){
			return false;
		}
		$category = $this->getCategories($id);
		
		return $category[0];
	}
		
	
	
	/**
	 * 编辑菜单
	 * @param array $data array('id'=>'','name'=>'','desc'=>'')
	 * @return boolean true or false
	 */
	public function editCategory($data){
		if(empty($data['id'])){
			return false;
		}
		$utime = time();
		$sql = "UPDATE `{$this->table}` SET `name`='{$data['name']}',`description`='{$data['description']}',`utime`='{$utime}' WHERE `id`='{$data['id']}' LIMIT 1";
		$result = $this->execute($sql);

		return $result;
	}
	
	/**
	 * 删除菜单
	 * @param int $id
	 * @return boolean true or false
	 */
	public function delCategory($id){
		if(empty($id)){
			return false;
		}
		$sql = "UPDATE `{$this->table}` SET `status`='0' WHERE `id`='{$id}' LIMIT 1";
		$result = $this->execute($sql);
		
		return $result;
	}
	
}